package main

import (
	"fmt"
	"github.com/gogap/logs"
	"strings"
	"yundian/internel/app/dao"
	"yundian/internel/pkg/dbs"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
)

func main() {
	dingo.InitViper("./configs/adm.yaml")
	dbs.NewMysqlComponent().Step()

	//userIdArr := dao.NewUserDao().QueryChildUserIds(127).Unwrap().([]int)
	//fmt.Println(userIdArr)

	//userIdArr := dao.NewUserDao().QueryChildUserIds(127).Unwrap().([]int)
	//placesIdArr := dao.NewUserDao().GetPlacesIds(127, userIdArr).Unwrap().([]int)
	userIdArr := dao.NewUserDao().QueryChildUserIds(401361).Unwrap().([]int)
	placesIdArr := dao.NewUserDao().GetPlacesIds(401361, userIdArr).Unwrap().([]int)
	fmt.Println(strings.Replace(strings.Trim(fmt.Sprint(userIdArr), "[]"), " ", ",", -1))
	fmt.Println(strings.Replace(strings.Trim(fmt.Sprint(placesIdArr), "[]"), " ", ",", -1))

	list := dao.NewUserDao().QueryParentList(401821, 0)
	fmt.Println(list)

	var orderListUse []struct {
		Id       int    `gorm:"column:id" json:"id"`
		UserId   int    `gorm:"column:user_id" json:"userId"`
		Openid   string `gorm:"column:openid" json:"-"`
		CreateAt string `gorm:"column:create_at" json:"createAt"`
	}
	orderSql := `SELECT o.id,o.user_id,o.openid,DATE_FORMAT( FROM_UNIXTIME( o.create_at ), '%Y-%m-%d' ) create_at FROM orders o LEFT JOIN prices p ON o.product_id=p.product_id
	WHERE o.is_draw<>1 AND o.state=5 AND p.pay_money>o.settlement_money
  	AND o.user_id IN ?`
	if db := global.Orm.Debug().Raw(orderSql, userIdArr).Find(&orderListUse); db.Error != nil {
		logs.Error(db.Error)
	}

	var orderListNoUse []struct {
		Id       int    `gorm:"column:id" json:"id"`
		UserId   int    `gorm:"column:user_id" json:"userId"`
		Openid   string `gorm:"column:openid" json:"-"`
		CreateAt string `gorm:"column:create_at" json:"createAt"`
	}
	orderNoUseSql := `SELECT o.id,o.user_id,o.openid,DATE_FORMAT( FROM_UNIXTIME( o.create_at ), '%Y-%m-%d' ) create_at FROM orders o LEFT JOIN prices p ON o.product_id=p.product_id
	WHERE o.is_draw<>1 AND o.state=5 AND p.pay_money=o.settlement_money
  	AND o.user_id IN ?`
	if db := global.Orm.Debug().Raw(orderNoUseSql, userIdArr).Find(&orderListNoUse); db.Error != nil {
		logs.Error(db.Error)
	}

	//targetAll := map[dingo.Time]int{}
	//targetUse := map[dingo.Time]int{}
	//targetRepeat := map[dingo.Time]int{}
	//targetOtherRepeat := map[dingo.Time]int{}
	//struct {
	//	All         int
	//	Use         int
	//	Repeat      int
	//	OtherRepeat int
	//}{}

	targetRepeatMapUse := map[string][]int{}
	for _, item := range orderListUse {
		if targetRepeatMapUse[item.CreateAt] == nil {
			targetRepeatMapUse[item.CreateAt] = []int{}
		}
		targetRepeatMapUse[item.CreateAt] = append(targetRepeatMapUse[item.CreateAt], item.Id)
	}

	targetRepeatMapNotUse := map[string][]int{}
	for _, item := range orderListNoUse {
		if targetRepeatMapNotUse[item.CreateAt] == nil {
			targetRepeatMapNotUse[item.CreateAt] = []int{}
		}
		targetRepeatMapNotUse[item.CreateAt] = append(targetRepeatMapNotUse[item.CreateAt], item.Id)
	}

	for t, ids := range targetRepeatMapUse {
		fmt.Println(t, len(ids))
	}

	fmt.Println("==============")

	for t, ids := range targetRepeatMapNotUse {
		fmt.Println(t, len(ids))
	}

	//fmt.Println(placesIdArr)
}
